package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.PerformanceBonus;
import com.amsp.oa.oademo.mapper.PerformanceBonusMapper;
import com.amsp.oa.oademo.service.PerformanceBonusService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class PerformanceBonusServiceImpl extends ServiceImpl<PerformanceBonusMapper, PerformanceBonus>
        implements PerformanceBonusService {

    @Override
    public IPage<PerformanceBonus> getPage(Integer current, Integer size, String applicant, String department,
            String position, String startDate, String endDate) {
        LambdaQueryWrapper<PerformanceBonus> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        wrapper.like(StringUtils.isNotBlank(applicant), PerformanceBonus::getApplicant, applicant)
                .like(StringUtils.isNotBlank(department), PerformanceBonus::getDepartment, department)
                .like(StringUtils.isNotBlank(position), PerformanceBonus::getPosition, position);

        // 处理申请时间范围
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            wrapper.between(PerformanceBonus::getApplyDate, startDate, endDate + " 23:59:59");
        }

        // 按申请时间倒序排序
        wrapper.orderByDesc(PerformanceBonus::getApplyDate);

        return page(new Page<>(current, size), wrapper);
    }
}